package service

import (
	"ginim/models"
	"github.com/gin-gonic/gin"
	"strconv"
	"text/template"
)

// GetIndex
// @Tags 首页
// @Success 200 {string} ok
// @Router /index [get]
func GetIndex(ctx *gin.Context) {
	ctx.JSON(200, gin.H{
		"message": "ok",
	})
}

// Home
// @Tags 首页
// @Success 200 {string} ok
// @Router /index [get]
func Home(ctx *gin.Context) {
	idx, err := template.ParseFiles("index.html", "web/views/chat/head.html")
	if err != nil {
		panic(err)
	}
	idx.Execute(ctx.Writer, "ok")
}

func ToChat(ctx *gin.Context) {
	idx, err := template.ParseFiles(
		"web/views/chat/index.html",
		"web/views/chat/head.html",
		"web/views/chat/foot.html",
		"web/views/chat/tabmenu.html",
		"web/views/chat/concat.html",
		"web/views/chat/group.html",
		"web/views/chat/profile.html",
		"web/views/chat/createcom.html",
		"web/views/chat/userinfo.html",
		"web/views/chat/main.html")
	if err != nil {
		panic(err)
	}

	user := models.User{}
	userId, err := strconv.Atoi(ctx.Query("userId"))
	token := ctx.Query("token")
	user.ID = uint(userId)
	user.Identity = token

	idx.Execute(ctx.Writer, user)
}

func Chat(ctx *gin.Context) {
	models.Chat(ctx.Writer, ctx.Request)
}
